<template>
  <div class="login-container">

    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="left">

      <div class="title-container">
        <h3 class="title">系统登录</h3>
      </div>
      <div v-if="isError" class="error">用户名或密码错误!</div>
      <el-form-item class="name" prop="username">
        <el-input
          v-model="loginForm.username"
          placeholder="登录名"
          name="username"
          type="text"
		  auto-complete="off"
        />
      </el-form-item>

      <el-form-item class="pw" prop="password">
        <el-input
		
          type="password"
          v-model="loginForm.password"
          placeholder="登录密码"
          name="password"
          @keyup.enter.native="handleLogin" />
      </el-form-item>

      <el-button :loading="loading" class="btn" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin"> 登 录 </el-button>

    </el-form>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'

export default {
  name: 'Login',
  components: {  },
  data() {
     const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message:'请输入用户名' }],
        password: [{ required: true, trigger: 'blur', message:'请输入密码' }]
      },
      loading: false,
      isError: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push(this.redirect || '/home')
          }).catch(() => {
            this.loading = false;
            this.isError = true;
          })
        } else {
          return false
        }
      })
    },

  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg:#283443;
  $light_gray:#999;
  $cursor: #333;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input{
      color: $cursor;
      &::first-line {
        color: $light_gray;
      }
    }
  }

  /* reset element-ui css */
  .login-container {
	  
    .el-input {
      display: inline-block;
      height: 47px;
      width: 100%;
	  margin-top:10px;
      input {
		width: 100%;
        background: rgba(255, 255, 255, 0.8);
        border: 0px;
		overflow: hidden;
		margin-bottom:34px;
        -webkit-appearance: none;
        border-radius:5px;
		text-indent:40px;
        height: 47px;
        caret-color: $cursor;
        &:-webkit-autofill {
          // -webkit-box-shadow: 0 0 0px 1000px rgba(255, 255, 255, 0.8) inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
		
		
      }
	  
    }
    .el-form-item {
      border-radius: 5px;
	
    }
	.pw input{
			background:rgba(255, 255, 255, 0.8) url(../../assets/images/pw.png) no-repeat 15px center; 
			color:#333;
			background-size:6%;
	}
	.name input{
			background:rgba(255, 255, 255, 0.8) url(../../assets/images/id.png) no-repeat 15px center; 
			background-size:6%;
			color:#333;
	}
	.btn{
		border-radius:5px;
		font-size:18px;
		margin-top:10px;
	
	}
	.el-form-item__error {
		  padding-top:8px;
	}
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background:url(../../assets/images/indexbg.jpg) no-repeat center center;
  background-size:cover;
  .login-form {
    position: absolute;
    right:16%;
    width:500px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin:15% auto;
  }
  .error{
	  margin-top:15px;
    padding:10px;
    color:#fff;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 5px;
      right: 0px;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 115px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
	font-size:18px;
  }
 
}
</style>
